package com.supor.aiot.mine.ui.wechat;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.api.WechatImpl;

public class WechatViewModel extends BaseViewModel {

    private MutableLiveData<Result> oauthResult = new MutableLiveData<>();

    private MutableLiveData<Result> oauthStatusResult = new MutableLiveData<>();

    public MutableLiveData<Result> getOauthResult() {
        return oauthResult;
    }

    public MutableLiveData<Result> getOauthStatusResult() {
        return oauthStatusResult;
    }

    public WechatViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void getWechatOauthStatus() {
        WechatImpl.getInstance().getWechatOauthStatus("weChat").subscribe(result -> {
            if (result.isSuccess()) {
                oauthStatusResult.setValue(new Result.Success<>(result.data));
            } else {
                oauthStatusResult.setValue(new Result.Error("getWechatOauthStatus failed"));
            }
        }, throwable -> {
            Logc.e("getWechatOauthStatus: " + throwable);
            oauthStatusResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    @SuppressLint("CheckResult")
    public void setWechatOauth(String isOauth) {
        WechatImpl.getInstance().setWechatOauth(isOauth).subscribe(result -> {
            int status = result.status;
            if (status == 200) {
                oauthResult.setValue(new Result.Success<>(isOauth));
            } else if (status == 201) {
                oauthResult.setValue(new Result.Success<>(result.data));
            } else {
                oauthResult.setValue(new Result.Error("setWechatOauth failed"));
            }
        }, throwable -> {
            Logc.e("setWechatOauth: " + throwable);
            oauthResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }
}
